package nettcp2;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

public class TCPServer {
	public static void main(String[] args) {
		BufferedReader br = null;
		ServerSocket ss = null;
		Socket socket = null;
		try {
			//创建服务器端的服务套接字
			ss = new ServerSocket(10000);
			//接收到一个连接的请求，等待有客户端来建立连接
			socket = ss.accept();
			//获得这个通道的输入输出流对象（字节流）
			InputStream in = socket.getInputStream();
			br = new BufferedReader(new InputStreamReader(in));
			StringBuffer stringBuffer = new StringBuffer();
			String line  = null;
			while ((line  = br.readLine())!=null) {
				stringBuffer.append(line);
				stringBuffer.append("\r\n");
				System.out.println("------------------------------");
				System.out.println(""+socket.getInetAddress().getHostAddress()+" 发送了："+stringBuffer);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				if (br!=null) {
					br.close();
				}
				if (ss!=null) {
					ss.close();
				}
				if (socket!=null) {
					socket.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
